package me.firstapp.bts;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.utils.ShaSign;

public class OpenApiUtils {

	private static Logger logger = LoggerFactory.getLogger(OpenApiUtils.class);

	private static final String ACCESS_TOKEN = "3243erttrt565rr3ertfgr5yfy66hgg";
	private static final String ACCESS_SECRET = "132fffrtthh55433grgddw24234";
	private static final String SERVER = "http://172.31.41.250:9158/weixin";

	public static void sendJinBiChangeMessage(RestTemplate restTemplate, Long jinBiAccountTraceId) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendJinBiChangeMessage.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&jinBiAccountTraceId=").append(jinBiAccountTraceId);
		shaSign.addParam("jinBiAccountTraceId", jinBiAccountTraceId);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendJinBiChangeMessage.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendJinBiChangeMessage.do接口成功");
		} else {
			logger.error("调用sendJinBiChangeMessage.do接口失败：" + msg);
		}
	}

	public static void sendLevelChangeMsg(RestTemplate restTemplate, String openId, Integer oldLevel,
			Integer newLevel) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendLevelChangeMsg.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&openId=").append(openId);
		url.append("&oldLevel=").append(oldLevel);
		url.append("&newLevel=").append(newLevel);
		shaSign.addParam("openId", openId);
		shaSign.addParam("oldLevel", oldLevel);
		shaSign.addParam("newLevel", newLevel);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendLevelChangeMsg.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendLevelChangeMsg.do接口成功");
		} else {
			logger.error("调用sendLevelChangeMsg.do接口失败：" + msg);
		}
	}

	public static void sendLevelExpireMsg(RestTemplate restTemplate, String openId) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendLevelExpireMsg.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&openId=").append(openId);
		shaSign.addParam("openId", openId);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendLevelExpireMsg.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendLevelExpireMsg.do接口成功");
		} else {
			logger.error("调用sendLevelExpireMsg.do接口失败：" + msg);
		}
	}
}
